$(document).ready(function(){
	try{
		$.jgrid.no_legacy_api = true;
		$.jgrid.useJSON = true;
		$.jgrid.formatter.integer.thousandsSeparator = ',';
		$.jgrid.formatter.number.thousandsSeparator = ',';
		$.jgrid.formatter.currency.thousandsSeparator = ',';
		
		$("#orderItemsTable").jqGrid({						
			url :  appRoot + '/packingItem/list/' + packingId + '?ajax=1',
			editurl : appRoot + '/packingItem/save?ajax=1&packingId=' + packingId,
			datatype : 'json',
			mtype : 'GET',
			jsonreader: {             
				repeatitems: false,
				id:'Id',
				root:'rows'
			}, 
			colNames : [ 'id','objectId','orderItemId','描述', '卡板号', '数量', '单位','产品编号', '箱数','本/箱','净重','毛重','体积','sumNet','sumGross'],
			colModel : [
	       {
				name : 'id',
				index : 'id',
				editable : true,
				hidden:true
				
			}, {
				name : 'objectId',
				index : 'objectId',
				editable : true,
				hidden:true
			}, {
				name : 'orderItemId',
				index : 'orderItemId',
				editable : true,
				hidden:true
			},{
				name : 'description',
				index : 'description',
				width : 160,
				align : 'left',
				resizable : false,
				editable : true,
				edittype:'text',
				sortable: false,
				editrules : {
					required : true
				}

			}, {
				name : 'palletNo',
				index : 'palletNo',
				width : 50,
				editable : true,
				edittype:'text',
				sortable: false,
				editrules : {
					required : true
				}
			},{
	            name : 'quantity',
	            index : 'quantity',
	            width : 60,
	            editable : true,
	            edittype:'text',
	            sortable: false,
	            editrules : {
	                required : true,
	                number : true
	            }
	        }, {
				name : 'unit',
				index : 'unit',
				width : 40,
				editable : true,
				edittype:'select',
				formatter:'select',
				editoptions : {
			        value :units
			    },
				editrules : {
					required : true
				}
			}, {
				name : 'productNo',
				index : 'productNo',
				width : 100,
				align : 'right',
				editable : true,
				edittype:'text',
				sortable: false,
				editrules : {
					required : true
				}							
			}, {
				name : 'packages',
				index : 'packages',
				width : 60,
				align : 'right',
				editable : true,
				sortable: false,
				editrules : {
					number : true
				}
			}, {
	            name : 'packageUnit',
	            index : 'packageUnit',
	            width : 50,
	            align : 'right',
	            editable : true,
	            sortable: false,
	            editrules : {
	                number : true
	            }
	        }, {
				name : 'netWeight',
				index : 'netWeight',
				width : 60,
				align : 'right',
				editable : true,
				sortable: false,
				editrules : {
					required : true,
					number : true
				}
			}, {
				name : 'grossWeight',
				index : 'grossWeight',
				width : 60,
				align : 'right',
				editable : true,
				sortable: false,
				editrules : {
					required : true,
					number : true
				}
			}, {
				name : 'measurement',
				index : 'measurement',
				width : 60,
				align : 'right',
				editable : true,
				sortable: false,
				editrules : {
					required : true,
					number : true
				}
			}, {
				name : 'sumNet',
				index : 'sumNet',
				width : 60,
				align : 'right',
				editable : false,
				hidden:true
			}, {
				name : 'sumGross',
				index : 'sumGross',
				width : 60,
				align : 'right',
				editable : false,
				hidden:true
			}
			],
			gridComplete: function(){
				var grid = $("#orderItemsTable");
				var sumQuantity = grid.jqGrid('getCol', 'quantity', false, 'sum'); 
				var sumPackages = grid.jqGrid('getCol', 'packages', false, 'sum'); 
				var sumNetWeight = grid.jqGrid('getCol', 'sumNet', false, 'sum'); 
				var sumMeasurement = grid.jqGrid('getCol', 'measurement', false, 'sum'); 
				var sumGrossWeight = grid.jqGrid('getCol', 'sumGross', false, 'sum'); 
				grid.jqGrid('footerData','set', {description:"合计:", quantity: sumQuantity,packages:sumPackages
					,netWeight:sumNetWeight,grossWeight:sumGrossWeight,measurement:sumMeasurement}); 
			},
			width: 730,
			height : 300,
			pager : $('#pager'),
			nowNum : 100,
			rowNum : 100,
			sortname : 'id',
			viewrecords : true,
			footerrow: true,
			shrinkToFit:false,
			caption : ''
		}).navGrid("#pager",{edit:packingEditable,add:false,del:packingEditable,search:false});
		
	}catch(e){}
	
	 $("#type").change(function(){
		 var type = $(this).val();
         if(type==9){
        	 $("#byOthers").show();
         }else{
        	 $("#byOthers").val("");
        	 $("#byOthers").hide();
         }
	 });
	 $("#type").change();
	 $(".colorbox").colorbox({iframe:true, innerWidth:750, innerHeight:460});
	 $("#orderId").change(function(){
        var orderId = $(this).val();
        if(orderId){
            if(confirm("需要导入这些订单的明细数据吗？")){
                $("#notice").html("数据导入中...").show();
                 $.ajax({
                     url: appRoot + "/orderItem/importToPacking/" + packingId +　"?orderIds=" + orderId + "&date=" + (new Date()).valueOf(),
                     type:'post',
                     dataType:'json',
                     success:function(msg){
                         if(msg.result){
                             $("#orderItemsTable").trigger("reloadGrid");
                             $("#notice").html("成功导入" + msg.result + "条订单明细");
                         }else{
                        	 $("#notice").html("没有明细需要导入");
                         }
                     }
                });
            }
        }
    });

});

function cumstomerFormValidate(){
	if($("#type").val()==9&&$("#byOthers").val().trim()==""){
		f_v_r +="运输类型不能为空\n";
	}
}